﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-17 12:29:05
// 负责人：liucx
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.DTO;

namespace OF.Component.DAL
{

    /// <summary>
    ///通知消息 数据访问层
    /// </summary>	
    internal partial class Message
    {

        /// <summary>
        /// 根据断言条件获取单个通知消息
        /// </summary>
        public MessageInfo QuerySingle(Expression<Func<MessageInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取通知消息列表
    	/// </summary>
	    public IList<MessageInfo> QueryList(Expression<Func<MessageInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<MessageInfo> SearchList(MessageCondition condition)
        {
            IList<MessageInfo> list = new List<MessageInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_message_search]");
            //设置参数
            db.AddInParameter(dbc, "@MessageTitle", DbType.String, condition.MessageTitle);
            db.AddInParameter(dbc, "@MessageSource", DbType.Int32, condition.MessageSource);
            db.AddInParameter(dbc, "@MessageType", DbType.Int32, condition.MessageType);
            db.AddInParameter(dbc, "@IsSend", DbType.Int32, condition.IsSend);
            db.AddInParameter(dbc, "@OPID", DbType.String, condition.OPID);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, condition.rows);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    list.Add(SQLServerProvider.Create<MessageInfo>(dr));
                }
            }
            condition.total = (int)db.GetParameterValue(dbc, "@OutCount");
            return list;
        }

        /// <summary>
        /// 分页查询信息
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="regStatus">状态</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总数</param>
        /// <returns></returns>
        public DataTable GetMessageTypePage(int userID, int messageType, int pageIndex, int pageSize, out int pageCount)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_Message_ApiSearch]");
            //设置参数
            db.AddInParameter(dbc, "@UserID", DbType.Int32, userID);
            db.AddInParameter(dbc, "@MessageType", DbType.Int32, messageType);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);
            DataSet ds = db.ExecuteDataSet(dbc);
            pageCount = (int)db.GetParameterValue(dbc, "@OutCount");
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            };
        }

        /// <summary>
        /// 设置消息已读
        /// </summary>
        /// <param name="messageID">消息ID</param>
        /// <returns></returns>
        public int SetMessageRead(long messageID)
        {
            string sql = "UPDATE MessagePool SET IsRead=2 WHERE MessageID=@MessageID AND IsActive=1";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sql);
            db.AddInParameter(dbc, "@MessageID", DbType.Int64, messageID);
            return db.ExecuteNonQuery(dbc);
        }

        /// <summary>
        /// 获取未读消息并设置未已读
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public DataTable GetMessagePush(int userID)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[cg_Message_PushSearch]");
            //设置参数
            db.AddInParameter(dbc, "@UserID", DbType.Int32, userID);
            DataSet ds = db.ExecuteDataSet(dbc);
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            };
        }

        /// <summary>
        /// 发送所有用户消息
        /// </summary>
        /// <param name="msgObj"></param>
        /// <returns></returns>
        public bool SendAllUserMessage(long msgId)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[cg_Message_Send]");
            //设置参数
            db.AddInParameter(dbc, "@MessageId", DbType.Int64, msgId);
            return db.ExecuteNonQuery(dbc) > 0;
        }
        

        /// <summary>
        /// 根据类型分组汇总未读消息
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public DataTable GetMessageTotalType(int userID)
        {
            string strSql = @"  SELECT  COUNT(T.MessagePoolID) CT, T.MessageType FROM MessagePool T WITH ( NOLOCK )
                                WHERE   T.IsRead = 1 AND T.IsActive=1  AND T.UserID = @UserID
                                GROUP BY T.MessageType";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserID", DbType.Int32, userID);
            DataSet ds = db.ExecuteDataSet(dbc);
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 查询未读消息汇总
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public int GetMessageTotal(int userID)
        {
            string strSql = @"  SELECT COUNT(T.MessagePoolID) CT FROM MessagePool T WITH(NOLOCK )
                                WHERE   T.IsRead = 1  AND T.UserID = @UserID";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserID", DbType.Int32, userID);
            object ds = db.ExecuteScalar(dbc);
            return Convert.ToInt32(ds);
        }

        /// <summary>
        /// 获取未读消息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllMessagePush()
        {
            string strSql = @"  SELECT  T.MessageID ,
                                        T1.MessageTitle ,
                                        T1.MessageDesc,
				                        T2.ClientID,
										T.UserID
                                FROM    MessagePool T WITH ( NOLOCK )
                                        LEFT JOIN Message T1 WITH ( NOLOCK ) ON T1.MessageID = T.MessageID
				                        LEFT JOIN  UserClienRela T2 WITH(NOLOCK) ON T.UserID=T2.UserID
                                WHERE   T.MessageStatus = 1 AND T.IsActive=1";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            DataSet ds = db.ExecuteDataSet(dbc);
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }
    }

}